package com.example.springbott.service;

import com.example.springbott.bean.*;
import com.example.springbott.bean.common.BaseParam;
import com.example.springbott.bean.zeng.*;
import com.example.springbott.mapper.*;
import com.example.springbott.util.Count;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author: zeng
 * @Description:
 * @date: 15:38 2022/11/20
 */
@Service
public class AdminGoodsServiceImpl implements AdminGoodsService {
    @Autowired
    MarketGoodsMapper goodsMapper;
    @Autowired
    MarketGoodsSpecificationMapper marketGoodsSpecificationMapper;
    @Autowired
    MarketGoodsAttributeMapper marketGoodsAttributeMapper;
    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;
    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    MarketBrandMapper brandMapper;

    @Override
    public AdminGoodsListVo goodsList(BaseParam baseParm, GoodsList goodsList) {

        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        if (goodsList.getGoodsId() != null) {
            criteria.andIdEqualTo(goodsList.getGoodsId());
        }
        if (goodsList.getGoodsSn() != null) {
            criteria.andGoodsSnEqualTo((goodsList.getGoodsSn()));
        }
        if (goodsList.getName() != null) {
            criteria.andNameLike("%" + goodsList.getName() + "%");
        }
        if (baseParm.getSort() != null) {
            marketGoodsExample.setOrderByClause(baseParm.getSort() + " " + baseParm.getOrder());
        }

        if (baseParm.getLimit() != Count.LIMIT_ZERO) {
            PageHelper.startPage(baseParm.getPage(), baseParm.getLimit());
        }
        criteria.andDeletedEqualTo(false);

        List<MarketGoods> marketGoods = goodsMapper.selectByExample(marketGoodsExample);
        PageInfo<MarketGoods> pageInfos = new PageInfo<>(marketGoods);

        AdminGoodsListVo<MarketGoods> goodsListVo = new AdminGoodsListVo<>();

        goodsListVo.setPage(baseParm.getPage());
        goodsListVo.setLimit(baseParm.getLimit());
        goodsListVo.setPages(pageInfos.getPages());

        goodsListVo.setTotal((int) pageInfos.getTotal());
        goodsListVo.setList(marketGoods);
        return goodsListVo;
    }

    @Override
    public void deleteGoods(MarketGoods marketGoods) {

        // goodsMapper.deleteByPrimaryKey(marketGoods.getId());直接删除

        //     逻辑删除
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        if (marketGoods.getName() != null) {
            criteria.andNameEqualTo(marketGoods.getName());
        }
        marketGoods.setDeleted(true);
        goodsMapper.updateByExampleSelective(marketGoods, marketGoodsExample);
    }

    //展示商品细节
    @Override
    public AdminGoodsDetailVo goodsDetail(Integer id) {
        AdminGoodsDetailVo goodsDetailVo = new AdminGoodsDetailVo();

        //通过商品id拿到商品规格表specifications
        MarketGoodsSpecificationExample marketGoodsSpecificationExample = new MarketGoodsSpecificationExample();
        MarketGoodsSpecificationExample.Criteria specificationExampleCriteria = marketGoodsSpecificationExample.createCriteria();
        specificationExampleCriteria.andGoodsIdEqualTo(id);
        List<MarketGoodsSpecification> goodsSpecifications = marketGoodsSpecificationMapper.selectByExample(marketGoodsSpecificationExample);
        goodsDetailVo.setSpecifications(goodsSpecifications);

        //通过商品id拿到商品参数表attributes
        MarketGoodsAttributeExample marketGoodsAttributeExample = new MarketGoodsAttributeExample();
        MarketGoodsAttributeExample.Criteria marketGoodsAttributeExampleCriteria = marketGoodsAttributeExample.createCriteria();
        marketGoodsAttributeExampleCriteria.andGoodsIdEqualTo(id);
        List<MarketGoodsAttribute> goodsAttributes = marketGoodsAttributeMapper.selectByExample(marketGoodsAttributeExample);
        goodsDetailVo.setAttributes(goodsAttributes);

        //通过商品id拿到商品库存表products
        MarketGoodsProductExample marketGoodsProductExample = new MarketGoodsProductExample();
        MarketGoodsProductExample.Criteria marketGoodsProductExampleCriteria = marketGoodsProductExample.createCriteria();
        marketGoodsProductExampleCriteria.andGoodsIdEqualTo(id);
        List<MarketGoodsProduct> goodsProducts = marketGoodsProductMapper.selectByExample(marketGoodsProductExample);
        goodsDetailVo.setProducts(goodsProducts);

        //通过商品id拿到商品信息Goods
        MarketGoods goods = goodsMapper.selectByPrimaryKey(id);
        goodsDetailVo.setGoods(goods);

        //拿到商品所属类目和类目的父类目
        Integer categoryId = goods.getCategoryId();
        MarketCategory marketCategory = marketCategoryMapper.selectByPrimaryKey(categoryId);
        Integer pid = marketCategory.getPid();
        List<Integer> list = new ArrayList<>();
        list.add(categoryId);
        list.add(pid);
        goodsDetailVo.setCategoryIds(list);

        return goodsDetailVo;
    }


    @Override
    public BaseRespVo getCatAndBrand() {
        List<InnerBrandOfCatAndBrandVO> brandList = brandMapper.selectValueAndLabel();
        // ArrayList<InnerCategoryOfCatAndBrandL1VO> categoryList = marketCategoryMapper.selectValueAndLabel();
        //
        ArrayList<InnerCategoryOfCatAndBrandL1VO> categoryList = new ArrayList<>();
        //拿到所以类目信息
        List<MarketCategory> categories = marketCategoryMapper.selectByExample(null);
        for (MarketCategory category : categories) {
            InnerCategoryOfCatAndBrandL1VO innerCategoryOfCatAndBrandL1VO = new InnerCategoryOfCatAndBrandL1VO();
            innerCategoryOfCatAndBrandL1VO.setValue(category.getId());
            innerCategoryOfCatAndBrandL1VO.setLabel(category.getName());
            // 遍历，根据父类目的id拿到所有子类目信息
            MarketCategoryExample categoryExample = new MarketCategoryExample();
            MarketCategoryExample.Criteria categoryExampleCriteria = categoryExample.createCriteria();
            categoryExampleCriteria.andPidEqualTo(category.getId());
            List<MarketCategory> categoriesChilds = marketCategoryMapper.selectByExample(categoryExample);
            //将子类目信息的id和name封装到children列表中
            ArrayList<InnerCategoryOfCatAndBrandL2VO> chlidren = new ArrayList<>();
            for (MarketCategory categoriesChild : categoriesChilds) {
                InnerCategoryOfCatAndBrandL2VO childrenVo = new InnerCategoryOfCatAndBrandL2VO();
                childrenVo.setValue(categoriesChild.getId());
                childrenVo.setLabel(categoriesChild.getName());
                chlidren.add(childrenVo);
            }
            innerCategoryOfCatAndBrandL1VO.setChildren(chlidren);
            categoryList.add(innerCategoryOfCatAndBrandL1VO);
        }
        //封装返回对象
        CatAndBrandVO catAndBrandVO = new CatAndBrandVO();
        catAndBrandVO.setBrandList(brandList);
        catAndBrandVO.setCategoryList(categoryList);

        BaseRespVo baseRespVo = BaseRespVo.ok(catAndBrandVO);
        return baseRespVo;
    }

    @Override
    public void goodsCreate(AdminGoodsCreateBo goodsCreateBo) {
        List<CreateOfAttributes> attributes = goodsCreateBo.getAttributes();
        CreateOfGoodsBO goods = goodsCreateBo.getGoods();
        List<CreateOfProducts> products = goodsCreateBo.getProducts();
        List<CreateOfSpecificationsBO> specifications = goodsCreateBo.getSpecifications();

        //插入attribute表
        for (CreateOfAttributes attribute : attributes) {
            marketGoodsAttributeMapper.insertAttributes(Integer.parseInt(goods.getGoodsSn()), attribute.getAttribute(), attribute.getValue());
        }

        //插入product表
        for (CreateOfProducts product : products) {
            product.setId(Integer.parseInt(goods.getGoodsSn()));
            product.setGoodsId(Integer.valueOf(goods.getGoodsSn()));
            product.setAddTime(new Date());
            product.setUpdateTime(new Date());
            product.setDeleted(false);
            marketGoodsProductMapper.insertProduct(product);
        }

        for (CreateOfSpecificationsBO specification : specifications) {
            specification.setId(null);
            specification.setGoodsId(Integer.valueOf(goods.getGoodsSn()));
            specification.setAddTime(new Date());
            specification.setUpdateTime(new Date());
            specification.setDeleted(false);
            marketGoodsSpecificationMapper.insertSpecification(specification);
        }
        goods.setId(Integer.valueOf(goods.getGoodsSn()));
        //goods还需要插入一些数值
        // goods.setSortOrder(goods);
        // goods.setShareUrl(goods.getShareUrl());

        goods.setRetailPrice(products.get(Count.ZERO).getPrice());
        goods.setAddTime(new Date());
        goods.setUpdateTime(new Date());
        goods.setDeleted(false);
        goodsMapper.insertGoods(goods);
    }

    @Override
    public void goodsUpdate(AdminGoodsCreateBo goodsCreateBo) {
        List<CreateOfAttributes> attributes = goodsCreateBo.getAttributes();
        CreateOfGoodsBO goods = goodsCreateBo.getGoods();
        List<CreateOfProducts> products = goodsCreateBo.getProducts();
        List<CreateOfSpecificationsBO> specifications = goodsCreateBo.getSpecifications();

        //更新商品
        goodsMapper.updateByPrimaryKeyByZeng(goods);
        //更新规格表
        for (CreateOfSpecificationsBO specification : specifications) {
            marketGoodsSpecificationMapper.updateByPrimaryKeyByZeng(specification);
        }
        //更新产品表
        for (CreateOfProducts product : products) {
            marketGoodsProductMapper.updateByPrimaryKeyByZeng(product);
        }
        //更新Attribute表
        for (CreateOfAttributes attribute : attributes) {
            // 可能有新增的attribute，执行增加insert操作
            if (attribute.getId() == null) {
                marketGoodsAttributeMapper.insertAttributesByZeng(goods.getId(), attribute.getAttribute(), attribute.getValue());
            }
            marketGoodsAttributeMapper.updateByPrimaryKeyByZeng(attribute);
        }

    }


}
